package com.woniuxy.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.DTO.AddAnimalTypeDTO;
import com.woniuxy.DTO.AlgsAnimalDTO;
import com.woniuxy.DTO.GoDownAnimalDTO;
import com.woniuxy.service.IAlgsCageService;
import com.woniuxy.vo.AlgsAnimalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniuxy.domain.AlgsAnimal;
import com.woniuxy.service.IAlgsAnimalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 动物库存管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-20
 */
@RestController
@RequestMapping("/animalManager")
public class AlgsAnimalController extends BaseController
{
    @Autowired
    private IAlgsAnimalService algsAnimalService;
@Resource
private IAlgsCageService iAlgsCageService;
    /**
     * 查询动物库存
     */
   // @RequiresPermissions("animalManager:animalManager:list")
    @GetMapping("/list")
    public TableDataInfo list(AlgsAnimalDTO algsAnimalDTO)
    {
        startPage();
        List<Object> list = algsAnimalService.selectAllAnimal(algsAnimalDTO);
        return getDataTable(list);
    }

    /**
     * 导出动物库存管理列表
     */
    @RequiresPermissions("animalManager:animalManager:export")
    @Log(title = "动物库存管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgsAnimal algsAnimal)
    {
        List<AlgsAnimal> list = algsAnimalService.selectAlgsAnimalList(algsAnimal);
        ExcelUtil<AlgsAnimal> util = new ExcelUtil<AlgsAnimal>(AlgsAnimal.class);
        util.exportExcel(response, list, "动物库存管理数据");
    }

    /**
     * 获取动物库存管理详细信息
     */
    @RequiresPermissions("animalManager:animalManager:query")
    @GetMapping(value = "/{animalId}")
    public AjaxResult getInfo(@PathVariable("animalId") Long animalId)
    {
        return success(algsAnimalService.selectAlgsAnimalByAnimalId(animalId));
    }

    /**
     * 新增动物库存管理
     */
    @RequiresPermissions("animalManager:animalManager:add")
    @Log(title = "动物库存管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddAnimalTypeDTO addAnimalTypeDTO)
    {
        int i = algsAnimalService.addAnimalType(addAnimalTypeDTO);
        if (i==i){
            return AjaxResult.success();
        }else if(i==0){
            return AjaxResult.error(999,"笼子不足");
        }else {
            return AjaxResult.error(998,"操作错误");
        }

    }

    /**
     * 动物出库管理,同时释放笼子
     */
    //@RequiresPermissions("animalManager:animalManager:edit")
    //@Log(title = "动物库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlgsAnimal algsAnimal)
    {
        if(algsAnimalService.updateAlgsAnimal(algsAnimal)!=0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }

    }

    /**
     * 删除动物库存管理
     */
    @RequiresPermissions("animalManager:animalManager:remove")
    @Log(title = "动物库存管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{animalIds}")
    public AjaxResult remove(@PathVariable Long[] animalIds)
    {
        return toAjax(algsAnimalService.deleteAlgsAnimalByAnimalIds(animalIds));
    }
    /**
     * 查询所有笼子的尺寸
     */
  //  @RequiresPermissions("animalManager:animalManager:remove")

    @GetMapping("queryCageSize")
    public AjaxResult queryCageSize()
    {
        return success(iAlgsCageService.selectAllCage());
    }

   // @RequiresPermissions("animalManager:animalManager:add")

    @PostMapping("/goDown")
    public AjaxResult goDown(@RequestBody GoDownAnimalDTO goDownAnimalDTO)
    {
        int i = iAlgsCageService.goDown(goDownAnimalDTO);
        if(i==1){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }

    }

}
